//
//  QYMoreNotificationsController.m
//  QingYue
//
//  Created by Zheng on 2017/6/23.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYMoreNotificationsController.h"
#import "QYMoreNotificationsCell.h"
#import "QYMoreNotificationsModel.h"

static NSString *QYMoreNotificationsCel_ID = @"QYMoreNotificationsCel_ID";

@interface QYMoreNotificationsController () <UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *_dataArr;
    UIButton *_deleteAllBtn;
}

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation QYMoreNotificationsController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 100;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"QYMoreNotificationsCell" bundle:nil] forCellReuseIdentifier:QYMoreNotificationsCel_ID];

    }
    return _tableView;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"通知";
    
    [self.view addSubview:self.tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.mas_equalTo(-70);
    }];
    
    _deleteAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_deleteAllBtn setTitle:@"清空所有记录" forState:UIControlStateNormal];
    _deleteAllBtn.frame = (CGRect){20,MSSize_Height-60,MSSize_Width-40,45};
    _deleteAllBtn.layer.backgroundColor = RGB(32, 203, 37, 1).CGColor;
    _deleteAllBtn.layer.cornerRadius = 3;
    _deleteAllBtn.titleLabel.font = Font(14);
    [_deleteAllBtn addTarget:self action:@selector(deleteAllRequest) forControlEvents:UIControlEventTouchUpInside];
    
    [[UIApplication sharedApplication].delegate.window addSubview:_deleteAllBtn];

    
    [self loadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [_deleteAllBtn removeFromSuperview];
    _deleteAllBtn = nil;
}

- (void)deleteAllRequest {
    [SVProgressHUD show];
    
    NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"]};
    
    [[YCRequestDataService sharedManager] Post:@"DelAllNews" parameters:param success:^(id response) {
        _dataArr = [NSMutableArray new];
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            NSArray *newsList = response[@"newsList"];
            [newsList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QYMoreNotificationsModel *model = [[QYMoreNotificationsModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            
            [SVProgressHUD dismissWithDelay:.35];
            
            [self loadData];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];

}

- (void)loadData {
    [SVProgressHUD show];
    
    NSDictionary *param = @{@"username":[kUserDefaults objectForKey:@"username"]};
    
    [[YCRequestDataService sharedManager] Post:@"GetNews" parameters:param success:^(id response) {
        _dataArr = [NSMutableArray new];
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            NSArray *newsList = response[@"newsList"];
            [newsList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QYMoreNotificationsModel *model = [[QYMoreNotificationsModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];
            
            [SVProgressHUD dismissWithDelay:.35];
            
            [self.tableView reloadData];
        } else {
            kTimeAfter(1, ^{kShowNet(response[@"Exception"]);});
        }
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
    }];

}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QYMoreNotificationsCell *cell = [tableView dequeueReusableCellWithIdentifier:QYMoreNotificationsCel_ID forIndexPath:indexPath];
    
    [cell reloadDataAction:^{
        [self loadData];
    }];
    
    QYMoreNotificationsModel *model = _dataArr[indexPath.section];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
